package com.wfw.second.controller;

import java.util.HashMap;
import java.util.Map;

/**
 * @ Author     ：duanchao
 * @ Date       ： 17:40 2021/1/11
 * @ Description：
 */
public class TestLeed {

    public static void main(String[] args) {
        int[] a ={2,7,11,15};
        int target = 9;
        int[] ints = twoSum1(a, target);
        System.out.println(ints[0]+"****"+ints[1]);

    }

    public static int[] twoSum(int[] nums, int target) {

        int[] r = new int[2];
        for (int i = 0; i < nums.length; i++) {
            for (int j = i+1; j < nums.length; j++) {
                if(nums[i]+nums[j]==target){
                    r[0]=i;
                    r[1]=j;
                    break;
                }

            }

        }
        return r;

    }

    public static int[] twoSum1(int[] nums, int target) {

        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
        if(map.containsKey(target-nums[i])){
            return  new int[]{map.get(target-nums[i]),i};
        }
        map.put(nums[i],i);

        }
        return  new int[]{0};
    }
}